﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;
using System.IO;

//using ColourAdvisorPhoneApp.ColourAdvisorServiceReference; // local host service reference
using ColourAdvisorPhoneApp.FashionColourAdvisorCloudServiceReference; // Azure cloud service reference


namespace ColourAdvisorPhoneApp
{
    public partial class ColourResultPage : PhoneApplicationPage
    {
        // method to get description attribute of ColourTypeName enums so displayed with spacing e.g. Deep Winter rather than DeepWinter
        public static string GetEnumDescription(Enum value)
        {
            System.Reflection.FieldInfo field = value.GetType().GetField(value.ToString());

            System.ComponentModel.DescriptionAttribute[] attributes =
                (System.ComponentModel.DescriptionAttribute[])field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }
        
        public ColourResultPage()
        {
            InitializeComponent();

            try
            {
                // Access App class for user colour type in isolated storage
                ColourTypeName colourTypeResult = (Application.Current as App).ColourType;

                try
                {
                    // method to get description attribute of ColourTypeResult
                    String name = GetEnumDescription(colourTypeResult);

                    // OR use Enum.GetName to get string value of the enum colourTypeResult  
                    // string name = Enum.GetName(typeof(ColourTypeName), colourTypeResult);     

                    string colourTypeText = name;
                    // display colour type to text block on phone screen
                    colourResultTextBlock.Text = colourTypeText;

                    // construct absolute Url to map to celebrity image in blob storage
                    String imageUrlCelebrity = colourTypeResult.ToString() + "_Celebrity.jpg";
                    // assign this Url to source property of image
                    celebrityImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlCelebrity, UriKind.Absolute));

                    // construct absolute Url to map to colour images in blob storage
                    String imageUrlColours = colourTypeResult.ToString() + "_Colours.jpg";
                    // assign this Url to source property of image
                    coloursImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlColours, UriKind.Absolute));
                }
                catch (WebException e)
                {
                    // display error message
                    MessageBox.Show(e.Message);
                }
            }
            catch (IsolatedStorageException ise)
            {
                // display error message
                MessageBox.Show(ise.Message);

                // // result is empty which means this is the first use of the app i.e. no previous result stored to isolated storage
                // MessageBox.Show("No previous result found. You need to select hair colour, eye colour and skin tone");
                // // redirect user to page where they select hair, eye colour and skin tone
                // NavigationService.Navigate(new Uri("/ColouringDetailsPage.xaml", UriKind.Relative));
            }
     

            //try
            //{
            //    // Access App class for user colour type in isolated storage
            //    ColourTypeName colourTypeResult = (Application.Current as App).ColourType;

            //    // if result is not empty i.e. result was saved to isolated storage
            //    if (colourTypeResult != null)
            //    {
            //        // method to get description attribute of ColourTypeResult
            //        String name = GetEnumDescription(colourTypeResult);

            //        // OR use Enum.GetName to get string value of the enum colourTypeResult  
            //        // string name = Enum.GetName(typeof(ColourTypeName), colourTypeResult);     

            //        string colourTypeText = name;
            //        // display colour type to text block on phone screen
            //        colourResultTextBlock.Text = colourTypeText;


            //        // construct absolute Url to map to celebrity image in blob storage
            //        String imageUrlCelebrity = colourTypeResult.ToString() + "_Celebrity.jpg";
            //        // assign this Url to source property of image
            //        celebrityImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlCelebrity, UriKind.Absolute));

            //        // construct absolute Url to map to colour images in blob storage
            //        String imageUrlColours = colourTypeResult.ToString() + "_Colours.jpg";
            //        // assign this Url to source property of image
            //        coloursImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlColours, UriKind.Absolute));
            //    }
            //    else
            //    {
            //        // result is empty which means this is the first use of the app i.e. no previous result stored to isolated storage
            //        // redirect user to page where they select hair, eye colour and skin tone
            //        NavigationService.Navigate(new Uri("/ColouringDetailsPage.xaml", UriKind.Relative));
            //    }
            //}
            //catch (Exception)
            //{
            //    // display error message
            //    MessageBox.Show("No previous result found. Select hair colour, eye colour and skin tone");
            //}                     
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // create proxy
                ColourTypeServiceClient client = new ColourTypeServiceClient();

                // Event handler for when web service call is finished. Event handler calls another method to display the result
                client.DisplayColourTypeDescriptionCompleted += new EventHandler<DisplayColourTypeDescriptionCompletedEventArgs>(client_DisplayColourTypeDescriptionCompleted);

                // Access App class for user colour type in isolated storage
                ColourTypeName colourTypeResult = (Application.Current as App).ColourType;

                // web service call, pass in user details to method
                client.DisplayColourTypeDescriptionAsync(colourTypeResult);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        // method to display result of web service call
        void client_DisplayColourTypeDescriptionCompleted(object sender, DisplayColourTypeDescriptionCompletedEventArgs e)
        {
            // if there are no errors 
            if (e.Error == null)
            {
                try
                {                    
                    // e.Result is an object of type ColourType with an id, name and description
                    ColourType colourTypeObject = e.Result;

                    // Save the colour type id to App class for isolated storage - 
                    // as will need this for another web service call to get fashion advice description
                    (Application.Current as App).ColourTypeId = e.Result.colourType_id;

                    // get colour type description property from the object
                    String colourDescription = colourTypeObject.colourType_description;

                    // bind this description to the colourDescriptionTextBlock
                    colourDescriptionTextBlock.Text = colourDescription;  
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                // display error message
                MessageBox.Show(e.Error.ToString());
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            // Navigate to previous page which displays dominant characteristic
            NavigationService.Navigate(new Uri("/ColourTypePage.xaml", UriKind.Relative));
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            // Navigate to next page which is Images for that Colour Type
            NavigationService.Navigate(new Uri("/ColourTypeImagesPage.xaml", UriKind.Relative));
        }

        private void IntroItem_Click(object sender, EventArgs e)
        {
            // Navigate to Intro page i.e. MainPage
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void DiscoverItem_Click(object sender, EventArgs e)
        {
            //Navigate to Colouring Details Page to Start "Discover My Colour Type"
            NavigationService.Navigate(new Uri("/ColouringDetailsPage.xaml", UriKind.Relative));
        }

        private void ColourTypeImagesItem_Click(object sender, EventArgs e)
        {
            // Navigate to My Colour Type Images page
            NavigationService.Navigate(new Uri("/ColourTypeImagesPage.xaml", UriKind.Relative));
        }

        private void FashionTrendsItem_Click(object sender, EventArgs e)
        {
            // Navigate to Fashion Trends page
            NavigationService.Navigate(new Uri("/FashionTrendsPage.xaml", UriKind.Relative));
        }
    }
}